package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.OfficeSave;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.OfficeSaveService;

/**
 * 到账登记Controller
 * @author wuh
 * @version 2016-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/officeSave")
public class OfficeSaveController extends BaseController {

	@Autowired
	private OfficeSaveService officeSaveService;
	
	@ModelAttribute
	public OfficeSave get(@RequestParam(required=false) String reserveid) {
		OfficeSave entity = null;
		if (StringUtils.isNotBlank(reserveid)){
			entity = officeSaveService.get(reserveid);
		}
		if (entity == null){
			entity = new OfficeSave();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:officeSave:view")
	@RequestMapping(value = {"list", ""})
	public String list(OfficeSave officeSave, HttpServletRequest request, HttpServletResponse response, Model model) {
		// 如果日期范围有空，则默认当前一个月
		if(StringUtils.isEmpty(officeSave.getBilldateStart()) ||
				StringUtils.isEmpty(officeSave.getBilldateEnd())) {
			Calendar calendar = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			//当前月的最后一天
			calendar.set(Calendar.DATE, 1);
			calendar.roll(Calendar.DATE, -1);
			officeSave.setBilldateEnd(sdf.format(calendar.getTime()));
			//当前月的第一天
			calendar.set(GregorianCalendar.DAY_OF_MONTH, 1);
			officeSave.setBilldateStart(sdf.format(calendar.getTime()));
		}
		Page<OfficeSave> page = officeSaveService.findPage(new Page<OfficeSave>(request, response), officeSave); 
		model.addAttribute("page", page);
		return "modules/rentproduct/officeSaveList";
	}

	@RequiresPermissions("rentproduct:officeSave:view")
	@RequestMapping(value = "form")
	public String form(OfficeSave officeSave, Model model) {
		model.addAttribute("officeSave", officeSave);
		return "modules/rentproduct/officeSaveForm";
	}

	@RequiresPermissions("rentproduct:officeSave:edit")
	@RequestMapping(value = "save")
	public String save(OfficeSave officeSave, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, officeSave)){
			return form(officeSave, model);
		}
		officeSaveService.save(officeSave);
		addMessage(redirectAttributes, "保存到账登记成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/officeSave/?repage";
	}

}